import Vue from 'vue'
import VueRouter from 'vue-router'
import login from '../component/login'
import Home from '../component/Home'
import test from '../component/test'
import Welcome from '../component/Welcome'
import Users from '../component/user/Users'
import Rights from '../component/power/Rights'
import Roles from '../component/power/Roles'
Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/test',
    component: test
  },
  {
    path: '/login',
    component: login
  },
  {
    path: '/home',
    component: Home,
    redirect: '/welcome',
    children: [{
      path: '/welcome',
      component: Welcome
    }, {
      path: '/users',
      component: Users
    }, {
      path: '/rights',
      component: Rights
    }, {
      path: '/roles',
      component: Roles
    }]
  }
]

const router = new VueRouter({
  routes
})

router.beforeEach((to, from, next) => {
  if (to.path === '/login') return next()

  const token = window.sessionStorage.getItem('token')
  if (!token) return next('/login')
  next()
})

export default router
